package com.xh.core.controller;

import com.xh.core.bean.AjaxResult;
import com.xh.core.constant.AjaxResultConstant;
import com.xh.core.properties.XhProperties;
import com.xh.core.util.OfficeUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.io.*;

/*
 * 公共转换接口
 * @Author daiwei
 * @Description
 * @Date 2018/12/19 17:12
 **/
@Api(tags="公共转换接口")
@RestController
@RequestMapping("/convert")
public class ConvertController {
    @Autowired
    ApplicationContext applicationContext;
    @Autowired
    private XhProperties xhProperties;

    /**
     * word文件转换html返回链接
     * @param path
     * @return
     */
    @ApiOperation(value="word文件转换html返回链接", notes = "word文件转换html返回链接")
    @ApiImplicitParams({
            @ApiImplicitParam(name="path", value="文件路径（目前仅支持本地文件路径）", required = true, dataType = "String"),
    })
    @PostMapping("/word2html")
    public AjaxResult word2html(String path, HttpServletRequest request){
        if(path.startsWith("http")){

        }else{
            String uploadPath=request.getSession().getServletContext().getRealPath("/");
            boolean flag=OfficeUtils.word2html(uploadPath+path,uploadPath+path+".html",null);
            return new AjaxResult(AjaxResultConstant.Result.SUCCESS,"",path+".html");
        }
        return new AjaxResult(AjaxResultConstant.Result.ERROR,"转换错误");
    }


    /**
     * word文件转换html返回链接
     * @param path
     * @return
     */
    @ApiOperation(value="word文件转换html返回链接", notes = "word文件转换html返回链接")
    @ApiImplicitParams({
            @ApiImplicitParam(name="path", value="文件路径（目前仅支持本地文件路径）", required = true, dataType = "String"),
    })
    @PostMapping("/word2htmlText")
    public AjaxResult word2htmlText(String path, HttpServletRequest request) throws Exception {
        if(path.startsWith("http")){

        }else{
            String uploadPath=request.getSession().getServletContext().getRealPath("/");
            boolean flag=OfficeUtils.word2html(uploadPath+path,uploadPath+path+".html",null);
            StringBuffer htmlContent = toHtmlString(uploadPath+path + ".html");
            return new AjaxResult(AjaxResultConstant.Result.SUCCESS,"",htmlContent);
        }
        return new AjaxResult(AjaxResultConstant.Result.ERROR,"转换错误");
    }

    public static StringBuffer toHtmlString(String filePath) throws Exception{
        File inputFile = new File(filePath);
        if(!inputFile.exists()){
            throw new Exception("没有找到文件！");
        }
        // 获取html文件流
        StringBuffer htmlSb = new StringBuffer();
        BufferedReader br =null;
        try {
            br = new BufferedReader(new InputStreamReader(new FileInputStream(filePath)));
            while(br.ready()){
                htmlSb.append(br.readLine());
            }
        } catch (FileNotFoundException e) {
            System.err.println("没有找到转换的HTML文件！");
        } catch (IOException e) {
            System.err.println("IO错误！");
        }finally {
            br.close();
        }
        return htmlSb;
    }
}
